<?php

class CCouponCodeUse extends Zend_Db_Table
{
	protected $_name;
	protected $_primary;

	public function __construct()
	{
		$this->_name = R_PRE."couponcode_use";
		$this->_primary = "id";
		parent::__construct();
	}
	
	public function isexist($code, $uid)
	{
		$db=$this->getAdapter();
		$where = $db->quoteInto('coupon = ?', $code).$db->quoteInto(' and uid = ?',$uid);

		return $this->fetchRow($where);
	}
	
	public function add($data)
	{
		return $this->insert($data);
	}
	public function isUsethanksCode($uid){
		
		$db=$this->getAdapter();
		$where = $db->quoteInto('coupon = ?', '13thanks').$db->quoteInto(' and status=?',0).$db->quoteInto(' and uid = ?', $uid);

		return $this->fetchRow($where);
	
	
	
	}
	/**
	 * 更新优惠码状态
	 * @param int $uid
	 * @param string $code
	 * @param int $status 1已使用 0 未使用
	 */
	public function updateCouponStatus($uid,$code,$status){
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('uid = ?', $uid).$db->quoteInto(' and coupon = ?',$code);
		$data=array('status'=>$status);
		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
				
			return false;
				
		}else{
				
			return true;
				
		}
	}
	public function getActivityCode($code,$uid,$status){
		$db=$this->getAdapter();
		$where = $db->quoteInto('coupon = ?',$code).$db->quoteInto(' and status= ? ',$status).$db->quoteInto('and uid= ? ', $uid);
		
		return $this->fetchRow($where);
	}
	/**
	 * 获取可用优惠码信息
	 * @param float $totalprice
	 */
	public function getCodeInfo($uid,$totalprice,$stauts){
		$codeInfo=array();
		if($totalprice>100.00){
			$code  = $this->getActivityCode('newjerseysoccerDISCOUNT',$uid,$stauts);
			if($code){
				$codeInfo['code']=$code->coupon;
				 $codeInfo['money']=20.00;
				 
			}else{
				$code  = $this->getActivityCode('13thanks',$uid,0);
				if($code){
					$codeInfo['code']=$code->coupon;
				 	$codeInfo['money']=5.00;
				}
				 
				 
			}
		}else if($totalprice>59.00){
			$code  = $this->getActivityCode('13thanks',$uid,0);
			if($code){
				$codeInfo['code']=$code->coupon;
				$codeInfo['money']=5.00;
			}
		}
		return $codeInfo;
	}
	
	
	
	
	
	
	
	
}